<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
* @property int $id 
* @property string $req_id 
* @property string $imei 
* @property string $oaid 
* @property string $useragent 
* @property string $referer 
* @property string $pkg 
* @property string $app_version
* @property string $os 
* @property string $os_version 
* @property string $model 
* @property string $net_type 
* @property string $carrier 
* @property string $progress 
* @property string $ad_id 
* @property string $ad_name 
* @property \Carbon\Carbon $created_at 
* @property \Carbon\Carbon $updated_at 
*/
class OcpcOppo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ocpc_oppo';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public const OPPO_YUJIANAI_CHANNEL = 404;
    public const OPPO_MOYU_CHANNEL = 1002;

//    public const CHANNELS = [self::OPPO_MOYU_CHANNEL, self::OPPO_YUJIANAI_CHANNEL];
    public const CHANNELS = [self::OPPO_MOYU_CHANNEL];

    /**
     * @param array $params
     * @return int
     */
    static function addLog(array $params = []): int
    {
        $oaid = $params['oaid'] ?? '';
        $channel = $params['c_id'] ?? self::CHANNELS[0];

        $data = [
            'channel_id'  => $channel,
            'req_id'      => $params['req_id'] ?? '',
            'imei'        => $params['imei'] ?? '',
            'oaid'        => $oaid,
            'useragent'   => $params['useragent'] ?? '',
            'referer'     => $params['referer'] ?? '',
            'pkg'         => $params['pkg'] ?? '',
            'app_version' => $params['app_version'] ?? '',
            'os'          => $params['os'] ?? '',
            'os_version'  => $params['os_version'] ?? '',
            'model'       => $params['model'] ?? '',
            'net_type'    => $params['net_type'] ?? '',
            'carrier'     => $params['carrier'] ?? '',
            'progress'    => $params['progress'] ?? '',
            'ad_id'       => $params['ad_id'] ?? '',
            'ad_name'     => $params['ad_name'] ?? '',
            'created_at'  => date('Y-m-d H:i:s'),
            'updated_at'  => date('Y-m-d H:i:s')
        ];

        return self::insertGetId($data);
    }

    /**
     * 根据oaid查询是否存在
     * @param string $oaid
     * @param int $channel
     * @return bool
     */
    public static function existsOaid(string $oaid, int $channel)
    {
        if (empty($oaid)) {
            return false;
        }

        return self::where('oaid', $oaid)->where('channel_id', $channel)->exists();
    }

    /**
     * 根据imei查询是否存在
     * @param string $imei
     * @param int $channel
     * @return bool
     */
    public static function existImei(string $imei, int $channel)
    {
        if (empty($imei)) {
            return false;
        }

        return self::where([
            'imei'    => $imei,
            'channel_id' => $channel
        ])->exists();
    }

    /**
     * 处理查询
     * @param $info
     * @return array|\Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object
     */
    public static function handleSearch($info)
    {
        //oaid 查询
        if (isset($info['oaid']) && $info['oaid']) {
            $res = self::where('oaid', $info['oaid'])
                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'channel_id', 'ad_id']);

            if ($res) {
                return $res->toArray();
            }
        }

        if (isset($info['imei']) && $info['imei']) {
            $res = self::where('imei', md5($info['imei']))
                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'channel_id', 'ad_id']);

            if ($res) {
                return $res->toArray();
            }
        }

        return [];
    }
}
